﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Threading;
using System.Drawing;

using TreeSharp;

using Styx;
using Styx.Logic;
using Styx.Logic.Pathing;
using Styx.WoWInternals;
using Styx.WoWInternals.WoWObjects;
using Styx.Helpers;


namespace FelMaster.Classes.Druid
{
    class DHXBoomkinMWB : TalentBase
    {
        public override string Name
        {
            get { return "DHX Boomkin (MWB Modded)"; }
        }
        
        private static int CurrentEclipse { get { return BitConverter.ToInt32(BitConverter.GetBytes(StyxWoW.Me.CurrentEclipse), 0); } }

        public override string KeySpell
        {
            get { return "StarSurge"; }
        }

        public override Composite SingleRotation
        {
            get {
                return new Sequence(
                    SC.BurstSequence(
                        new Decorator(
                            ret => Me.GotTarget && SC.CanCast("Force of Nature"),
                            new Sequence(
                                new TreeSharp.Action(ret => SC.CastSpell("Force of Nature")),
                                new TreeSharp.Action(ret => Styx.Logic.Combat.LegacySpellManager.ClickRemoteLocation(WoWMathHelper.CalculatePointFrom(Me.Location, Me.CurrentTarget.Location,Math.Max(0, Me.CombatReach+0.33333f + Me.CurrentTarget.CombatReach) - 3f)))
                            )
                        ),
                        SC.CastSpell("Starfall", a => true, "Starfall")
                    ),
                    
                    new PrioritySelector(
                        new Decorator(x => SC.PlayerIsChanneling, new TreeSharp.Action(a => SC.Log("Channeling"))),
                        SC.UseEngineerGloves(a => true, "Engineer's glove enchant"),
                        SC.CastSpell("Lifeblood", a => true, "Lifeblood"),
                        SC.CastSpell("Berserking", a => true, "Berserking"),
                        #region BurnMoving
                        new Decorator(
                            ret => Me.IsMoving,
                            new PrioritySelector(
                                 SC.RunMacroText("/Cast Starsurge", a => SC.PlayerHasBuff("Shooting Stars"), "Starsurge"),
                                 SC.CastSpell("Insect Swarm", a => !SC.TargetHasDebuff("Insect Swarm") || (SC.TargetHasDebuff("Insect Swarm") && SC.TargetDebuffTimeLeft("Insect Swarm") < 1), "DOT Insect Swarm"),
                                 SC.CastSpell("Moonfire", a => true, "Moonfire/Sunfire")
                            )
                        ),
                        #endregion
                        SC.CastSpell("Innervate",a =>  Me.ManaPercent < 50,"Innervate"),
                        SC.CastSpell("Insect Swarm", a => !SC.TargetHasDebuff("Insect Swarm") || (SC.TargetHasDebuff("Insect Swarm") && SC.TargetDebuffTimeLeft("Insect Swarm") < 1), "DOT Insect Swarm"),
                        SC.CastSpell("Moonfire", a => (SC.PlayerHasBuff("Eclipse (Lunar)") || SC.PlayerHasBuff("Eclipse (Solar)")) && (CurrentEclipse == 100 || CurrentEclipse == -100), "Proc Nature's Grace - Solar"),
                        SC.CastSpell("Moonfire", a => (SC.TargetDebuffTimeLeft("Moonfire") < 1 && SC.TargetDebuffTimeLeft("Sunfire") < 1), "DOT Moonfire/Sunfire"),
                        SC.CastSpell("Starsurge", a => true, "Starsurge"),
                        SC.CastSpell("Wrath", a => EclipseSolarCD || EclipseLunarRamp, "Wrath"),
                        SC.CastSpell("Starfire", a => EclipseLunarCD || EclipseSolarRamp, "Starfire")
                    )
                );
            }
        }

        public override Composite Buffs
        {
            get
            {
                return new PrioritySelector(
                    CastPartyBuff("Mark of the Wild")
                );
            }
        }

        public override Composite PVPRotation
        {
           get { throw new NotImplementedException(); } //leave it as it is, unless you want to code a PVP specific rotation
        }

        public override Composite PVERotation
        {
            get
            {
                return SingleRotation;
            }
        }
        public bool EclipseSolarOnCD = false;
        public bool EclipseLunarOnCD = false;
        public bool EclipseSolarBuildUp = false;
        public bool EclipseLunarBuildUp = false;
        private bool EclipseSolarCD
        {
            get
            {
                if (Me.HasAura("Eclipse (Solar)"))
                {
                    EclipseSolarOnCD = false;
                    return true;
                }
                return false;
            }
        }
        private bool EclipseSolarRamp
        {
            get
            {
                if (Me.CurrentEclipse <= 100 && !Me.HasAura("Eclipse (Solar)"))
                {
                    EclipseSolarBuildUp = true;
                    return true;
                }
                return false;
            }
        }
        private bool EclipseLunarRamp
        {
            get
            {
                if (Me.CurrentEclipse >= 101 && !Me.HasAura("Eclipse (Lunar)"))
                {
                    EclipseLunarBuildUp = true;
                    return true;
                }
                return false;
            }
        }
        private bool EclipseLunarCD
        {
            get
            {
                if (Me.HasAura("Eclipse (Lunar)"))
                {
                    EclipseLunarOnCD = false;
                    return true;
                }
                return false;
            }
        }
        private static Composite CastPartyBuff(string buff)
        {
            return
                new PrioritySelector(
                    SC.CastBuff(buff,
                        ret =>
                        {
                            var players = StyxWoW.Me.IsInParty
                                              ? StyxWoW.Me.PartyMembers.Union(new List<Styx.WoWInternals.WoWObjects.WoWPlayer> { StyxWoW.Me })
                                              : StyxWoW.Me.IsInRaid
                                                    ? StyxWoW.Me.RaidMembers.Union(new List<Styx.WoWInternals.WoWObjects.WoWPlayer> { StyxWoW.Me })
                                                    : new List<Styx.WoWInternals.WoWObjects.WoWPlayer> { StyxWoW.Me };

                            var result = players.Any(
                                p => p.DistanceSqr < 40 * 40 &&
                                     (!p.HasAura(buff) || p.Auras[buff].CreatorGuid != StyxWoW.Me.Guid));

                            return result;
                        }, buff)
                    );
        }
        public static bool AuraChkByID(uint ID) ///returns true if you have aura with WowHead id ID.
        {
            Styx.WoWInternals.ObjectManager.Update();
            List<Styx.Logic.Combat.WoWAura> GetBuffs = Me.GetAllAuras();
            foreach (Styx.Logic.Combat.WoWAura Buff in GetBuffs)
            {
                if (Buff.SpellId == ID)
                    return true;
            }
            return false;
        }
    }
}
